package com.member.web.controller.shop;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.member.shop.domain.SysBizProfile;
import com.member.shop.domain.SysBizRecharge;
import com.member.shop.domain.SysBizRecommendGoods;
import com.member.shop.service.ISysBizProfileService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.member.common.annotation.Log;
import com.member.common.core.controller.BaseController;
import com.member.common.core.domain.AjaxResult;
import com.member.common.enums.BusinessType;
import com.member.shop.domain.SysBizProfileCategory;
import com.member.shop.service.ISysBizProfileCategoryService;
import com.member.common.utils.poi.ExcelUtil;
import com.member.common.core.page.TableDataInfo;

/**
 * 【资料库分类】控制器
 *
 * @author lixinchen
 * @version 1.0
 * @date 2024-05-08
 **/
@RestController
@RequestMapping("/shop/profileCategory")
public class SysBizProfileCategoryController extends BaseController {
    @Resource
    private ISysBizProfileCategoryService sysBizProfileCategoryService;

    private void setTenantId(SysBizProfileCategory sysBizProfileCategory) {
        Long tenantId = getTenantId();
        if (tenantId != null && tenantId > 0) {
            sysBizProfileCategory.setTenantId(tenantId);
        }
    }

    @Resource
    private ISysBizProfileService sysBizProfileService;

    /**
     * 查询资料库分类列表
     */
    @PreAuthorize("@ss.hasPermi('shop:profileCategory:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysBizProfileCategory sysBizProfileCategory) {
        startPage();
        setTenantId(sysBizProfileCategory);
        List<SysBizProfileCategory> list = sysBizProfileCategoryService.selectSysBizProfileCategoryList(sysBizProfileCategory);
        return getDataTable(list);
    }

    @GetMapping("/list2")
    public TableDataInfo list2(SysBizProfileCategory sysBizProfileCategory) {
        setTenantId(sysBizProfileCategory);
        List<SysBizProfileCategory> list = sysBizProfileCategoryService.selectSysBizProfileCategoryList(sysBizProfileCategory);
        return getDataTable(list);
    }

    /**
     * 导出资料库分类列表
     */
    @PreAuthorize("@ss.hasPermi('shop:profileCategory:export')")
    @Log(title = "资料库分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysBizProfileCategory sysBizProfileCategory) {
        setTenantId(sysBizProfileCategory);
        List<SysBizProfileCategory> list = sysBizProfileCategoryService.selectSysBizProfileCategoryList(sysBizProfileCategory);
        ExcelUtil<SysBizProfileCategory> util = new ExcelUtil<SysBizProfileCategory>(SysBizProfileCategory.class);
        util.exportExcel(response, list, "资料库分类数据");
    }

    /**
     * 获取资料库分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('shop:profileCategory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysBizProfileCategoryService.selectSysBizProfileCategoryById(id));
    }

    /**
     * 新增资料库分类
     */
    @PreAuthorize("@ss.hasPermi('shop:profileCategory:add')")
    @Log(title = "资料库分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBizProfileCategory sysBizProfileCategory) {
        LambdaQueryWrapper<SysBizProfileCategory> sysBizProfileCategoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysBizProfileCategoryLambdaQueryWrapper.eq(SysBizProfileCategory::getProfileCategoryName, sysBizProfileCategory.getProfileCategoryName());
        sysBizProfileCategoryLambdaQueryWrapper.eq(SysBizProfileCategory::getType, sysBizProfileCategory.getType());
        if (!sysBizProfileCategoryService.selectSysBizProfileCategoryList(sysBizProfileCategoryLambdaQueryWrapper).isEmpty()) {
            return AjaxResult.warn("资料库分类名称已存在");
        }
        return toAjax(sysBizProfileCategoryService.insertSysBizProfileCategory(sysBizProfileCategory));
    }

    /**
     * 修改资料库分类
     */
    @PreAuthorize("@ss.hasPermi('shop:profileCategory:edit')")
    @Log(title = "资料库分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBizProfileCategory sysBizProfileCategory) {

        LambdaQueryWrapper<SysBizProfileCategory> sysBizProfileCategoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysBizProfileCategoryLambdaQueryWrapper.eq(SysBizProfileCategory::getProfileCategoryName, sysBizProfileCategory.getProfileCategoryName());
        sysBizProfileCategoryLambdaQueryWrapper.eq(SysBizProfileCategory::getType, sysBizProfileCategory.getType());
        sysBizProfileCategoryLambdaQueryWrapper.ne(SysBizProfileCategory::getId, sysBizProfileCategory.getId());
        if (!sysBizProfileCategoryService.selectSysBizProfileCategoryList(sysBizProfileCategoryLambdaQueryWrapper).isEmpty()) {
            return AjaxResult.warn("资料库分类名称已存在");
        }

        //查看是否分配了
        LambdaQueryWrapper<SysBizProfile> sysBizProfileLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysBizProfileLambdaQueryWrapper.eq(SysBizProfile::getCategoryId, sysBizProfileCategory.getId());
        List<SysBizProfile> sysBizProfiles = sysBizProfileService.selectSysBizProfileList(sysBizProfileLambdaQueryWrapper);
        //查看是否分配了
        if (!CollectionUtils.isEmpty(sysBizProfiles)) {
            //该资料库已分配,查看是否修改了类型 不允许编辑类型了
            SysBizProfileCategory sysBizProfileCategory1 = sysBizProfileCategoryService.selectSysBizProfileCategoryById(sysBizProfileCategory.getId());
            if (!sysBizProfileCategory1.getType().equals(sysBizProfileCategory.getType())) {
                return AjaxResult.warn("该资料库已分配,不允许修改类型");
            }
        }


        return toAjax(sysBizProfileCategoryService.updateSysBizProfileCategory(sysBizProfileCategory));
    }

    /**
     * 删除资料库分类
     */
    @PreAuthorize("@ss.hasPermi('shop:profileCategory:remove')")
    @Log(title = "资料库分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        for (Long id : ids) {
            LambdaQueryWrapper<SysBizProfile> sysBizProfileLambdaQueryWrapper = new LambdaQueryWrapper<>();
            sysBizProfileLambdaQueryWrapper.eq(SysBizProfile::getCategoryId, id);
            List<SysBizProfile> sysBizProfiles = sysBizProfileService.selectSysBizProfileList(sysBizProfileLambdaQueryWrapper);
            if (!CollectionUtils.isEmpty(sysBizProfiles)) {
                return AjaxResult.warn("该资料库已分配,不允许删除");
            }
        }
        return toAjax(sysBizProfileCategoryService.deleteSysBizProfileCategoryByIds(ids));
    }
}
